﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Website_Module
{
    public class Website
    {
        IList<AMP_Portal_Website> Portals = new List<AMP_Portal_Website>();
        IList<AMP_Assignment_Website> Assignment_Webs = new List<AMP_Assignment_Website>();
        IList<AMP_Subject_Website> Subject_Webs = new List<AMP_Subject_Website>();

        public int Make_Assignment(AMP_Assignment_Website assign)//Note for ASP team: Server needs to implement upload first before this method
        {
            AMP_Assignment_Website AWeb = new AMP_Assignment_Website();// creates a new instance
            AWeb = assign; //assigns all the properties of assign to AWeb
            Assignment_Webs.Add(AWeb);
            SaveOrUpdateAssignment(AWeb);
            return AWeb.AssignmentID;//AssignmentID
        }

        public void Make_Assignments_Portal(int pID, string portalname, IList<AMP_User_Website> users, int assignID)
        {
            foreach (AMP_Portal_Website p in Portals)
            {
                if (p.PortalID != pID)
                {
                    p.PortalID = pID;
                    p.PortalName = portalname;
                    p.AssignmentID = assignID;
                    foreach (AMP_User_Website user in users)
                    {
                        p.Users.Add(user); // adds each user specified in the parameters to the users in the portal class
                    }
                    Portals.Add(p);
                    SaveOrUpdatePortal(p);
                    break;
                }
                
            }
            
        }

        public void Add_User_To_Portal(AMP_Portal_Website port, AMP_User_Website user)
        {
            foreach (AMP_Portal_Website p in Portals)
            {//searches for the users portal ID
                if (p.PortalID == port.PortalID)
                {//and adds the user to the correct portal id
                    p.Users.Add(user);
                }
                
            }
            
        }

        public void Remove_user(AMP_User_Website user, int portID)
        {
            foreach (AMP_Portal_Website p in Portals)
            {//searches for the users portal ID
                if (p.PortalID == portID)
                {//and removes the user to the correct portal id
                    p.Users.Remove(user);
                }

            }
        }

        public void Remove_Portal(AMP_Portal_Website port)
        {
            foreach (AMP_Portal_Website p in Portals)
            {//searches for the portal id and removes it if found
                if (p.PortalID == port.PortalID)
                {
                    Portals.Remove(p);
                    DeletePortal(p);
                }
            }
        }

        public IList<AMP_Assignment_Website> Assigments(int UserID)
        {
            IList<AMP_Assignment_Website> lst = new List<AMP_Assignment_Website>();

            foreach (AMP_Assignment_Website AWeb in GetAssignments())
            {//searches thru all the assignments for the ones with the same user id
                if (AWeb.UserID == UserID)
                {//adds the assignment website to the list
                    lst.Add(AWeb);
                }
            }
            return lst;
        }

        public void Add_Subject_Year(int subID, int subYear)
        {
            IList<AMP_Subject_Website> lst = new List<AMP_Subject_Website>();
            lst = GetSubjects();
            foreach (AMP_Subject_Website s in lst)
            {
                if (s.SubjectId == subID)
                {
                    s.SubjectYear = subYear;
                    SaveOrUpdateSubject(s);
                }
            }
        }

        public void Change_Subject(AMP_Subject_Website sub)
        {
            IList<AMP_Subject_Website> lst = new List<AMP_Subject_Website>();
            lst = GetSubjects();
            foreach (AMP_Subject_Website s in lst)
            {
                if (s.SubjectId == sub.SubjectId)
                {
                    SaveOrUpdateSubject(sub);
                }
            }
        }

        public void Remove_Subject(AMP_Subject_Website sub)
        {
            foreach (AMP_Subject_Website SWeb in Subject_Webs)
            {
                if (SWeb.SubjectId == sub.SubjectId)
                {
                    Subject_Webs.Remove(SWeb);
                    DeleteSubject(sub);
                }
            }
        }

        public IList<AMP_Subject_Website> Get_User_Subject(int UserID)
        {
            IList<AMP_Subject_Website> lst = new List<AMP_Subject_Website>();
            foreach (AMP_Subject_Website SWeb in Subject_Webs)
            {
                foreach (AMP_User_Website user in SWeb.Users) // selects all the users in the current subject
                {
                    if (user.UserID == UserID)
                    {
                        lst.Add(SWeb); // adds the subject of the current user to the list
                    }
                }
            }
            return lst;
        }



    }
}
